<script>
	// #ifdef APP-PLUS
		import {
			registerRequestPermissionTipsListener,
			unregisterRequestPermissionTipsListener,
			setRequestPermissionTips
		} from "@/uni_modules/uni-registerRequestPermissionTips"
		const PermissionTips = {
			// 位置权限
			"android.permission.ACCESS_FINE_LOCATION": "<h4 style=\"font-size:40px;\">正在读取位置权限</h4><font color=#cccccc>我们需要获取您的位置信息，用来展示您附近的内容</font>",
			"android.permission.ACCESS_COARSE_LOCATION": "<h4 style=\"font-size:40px;\">正在读取位置权限</h4><font color=#cccccc>我们需要获取您的位置信息，用来展示您附近的内容</font>",
		
			// 读写sd卡权限
			"android.permission.WRITE_EXTERNAL_STORAGE": "<h4 style=\"font-size:40px;\">正在请求写入存储权限</h4><font color=#666666>需要申请您的图片权限，用于上传图片和视频</font>",
			"android.permission.READ_EXTERNAL_STORAGE": "<h4 style=\"font-size:40px;\">正在请求读取存储权限</h4><font color=#666666>需要申请您的图片权限，用于上传图片和视频</font>",
		
			// 读取权限
			"android.permission.READ_MEDIA_IMAGES": "<h4 style=\"font-size:40px;\">读取照片权限</h4><font color=#666666>需要申请读取照片权限，用于选择照片</font>",
			"android.permission.READ_MEDIA_VIDEO": "<h4 style=\"font-size:40px;\">读取视频权限</h4><font color=#666666>需要申请读取视频权限，用于上传视频</font>",
		
			// 相机权限
			"android.permission.CAMERA": "<h4 style=\"font-size:40px;\">正在请求相机权限</h4><font color=#666666>需要申请您的相机权限，用于拍摄照片或录制视频</font>",
		
			// 电话权限
			"android.permission.CALL_PHONE": "<h4 style=\"font-size:40px;\">正在请求拨打电话权限</h4><font color=#666666>需要申请您的拨打电话权限，用于联系用户</font>",
		
			// 录音权限
			"android.permission.RECORD_AUDIO": "<h4 style=\"font-size:40px;\">正在请求录音权限</h4><font color=#666666>需要申请您的录音权限，用于录制音频</font>",
		
		}
	// #endif
	export default {
		onLaunch: function() {
			uni.hideTabBar()
			console.log('App Launch')
			// #ifdef APP-PLUS
			plus.screen.lockOrientation('portrait-primary');
			var brand = uni.getSystemInfoSync().deviceBrand
			setRequestPermissionTips(PermissionTips)
			registerRequestPermissionTipsListener({
				onRequest: (e) => {
					console.log(e)
				},
				onConfirm: (e) => {
					console.log(e)
				},
				onComplete: (e) => {
					// 华为手机在权限禁止之后，再次申请权限不会出现权限申请框。此时应该引导用户去系统设置开启此权限，不应该频繁申请。
					if (brand.toLowerCase() == "huawei") {
						var tips = {}
						var hasDeniedPermission = false
						for (var k in PermissionTips) {
							if (e[k] != "denied") {
								tips[k] = PermissionTips[k]
							} else {
								hasDeniedPermission = true
							}
						}
						setRequestPermissionTips(tips) // 更新弹框提醒，防止华为手机不出现权限申请框时权限提醒框闪烁的情况
						// if (hasDeniedPermission)
						// 	uni.showModal({
						// 		content: "权限已经被拒绝，请前往设置中开启"
						// 	})
					}
				}
			})
			
			if (uni.getStorageSync('user')) {
				uni.switchTab({
					url: '/pages/index/index',
					success: () => {
						// #ifdef APP-PLUS
						setTimeout(()=>{
							plus.navigator.closeSplashscreen()
						},1000)
						// #endif
					}
				})
			} else {
				plus.navigator.closeSplashscreen()
			}
			// #endif
			
			
			// #ifdef H5
				uni.switchTab({
					url: '/pages/index/index'
				})
			// #endif
			
		},
		onExit: function() {
			// #ifdef APP-PLUS
			unregisterRequestPermissionTipsListener(null)
			// #endif
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			tt(title = '', icon = 'none') {
				uni.showToast({
					title,
					icon
				})
			},
			tologin() {
				uni.navigateTo({
					url: "/pages/Login/Login"
				})
			},

			getlocation(force = false) {
				return new Promise((resolve, rej) => {
					if (!uni.getStorageSync('user')) {
						uni.setStorageSync('location', {
							lng: 0,
							lat: 0,
							// #ifdef APP-PLUS
							"country": "",
							"province": "",
							"city": "",
							"district": "",
							"street": "",
							"streetNum": "",
							"poiName": "",
							"cityCode": ""
							// #endif
						})
						resolve({
							lng: 0,
							lat: 0,
							// #ifdef APP-PLUS
							"country": "",
							"province": "",
							"city": "",
							"district": "",
							"street": "",
							"streetNum": "",
							"poiName": "",
							"cityCode": ""
							// #endif
						})
						return
					}
					const now = Date.now();
					const last = uni.getStorageSync('last') || 0;
					let interval = 10 * 60 * 1000; // 10分钟
					const location = uni.getStorageSync('location')
					if (now - last > interval || location?.lat == 0 || force) {
						let geocode = false;
						uni.getLocation({
							type: 'gcj02',
							// #ifdef APP-PLUS
							geocode: true, // app中返回位置信息
							// #endif
							success: (res) => {
								console.log('定位成了', res)
								uni.setStorageSync('location', {
									lng: res.longitude,
									lat: res.latitude,
									// #ifdef APP-PLUS
									...res.address
									// #endif
			
									// #ifdef H5
									"country": "中国",
									"province": "河南省",
									"city": "郑州市",
									"district": "二七区",
									"street": "长江路",
									"streetNum": "186号",
									"poiName": "亚星时代广场",
									"cityCode": "0371"
									// #endif
								})
								uni.setStorageSync('last', now)
								resolve({
									lng: res.longitude,
									lat: res.latitude,
									// #ifdef APP-PLUS
									...res.address
									// #endif
									// #ifdef H5
									"country": "中国",
									"province": "河南省",
									"city": "郑州市",
									"district": "二七区",
									"street": "长江路",
									"streetNum": "186号",
									"poiName": "亚星时代广场",
									"cityCode": "0371"
									// #endif
								})
							},
							fail: (err) => {
								console.log('定位失败', err)
								uni.setStorageSync('location', {
									lng: 0,
									lat: 0,
									// #ifdef APP-PLUS
									"country": "",
									"province": "",
									"city": "",
									"district": "",
									"street": "",
									"streetNum": "",
									"poiName": "",
									"cityCode": ""
									// #endif
								})
								resolve({
									lng: 0,
									lat: 0,
									// #ifdef APP-PLUS
									"country": "",
									"province": "",
									"city": "",
									"district": "",
									"street": "",
									"streetNum": "",
									"poiName": "",
									"cityCode": ""
									// #endif
								})
							}
						});
					} else {
						console.log('返回的本地缓存')
						resolve(location)
					}
				})
			},
		}
	}
</script>
<style>
	/*每个页面公共css */
	@import url("@/static/css/flex.css");
	@import url("@/static/css/myfont.css");

	page,
	view,
	image,
	text,
	input,
	button,
	textarea,
	scroll-view,
	swiper,
	swiper-item,
	movable-area,
	movable-view,
	navigator {
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		/* #endif */
		margin: 0;
		padding: 0;
	}

	/* 底部安全区域适配 */
	.iphonex {
		box-sizing: content-box;
		padding-bottom: env(safe-area-inset-bottom);
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99;
		width: 100%;
	}

	.o-h {
		overflow: hidden;
	}

	.c-8f {
		color: #8F9FB6 !important;
	}

	.c-main {
		color: #01796d !important;
	}

	.big-btn {
		width: 686rpx;
		height: 92rpx;
		background: #01796d;
		border-radius: 50rpx;
		font-size: 32rpx;
		color: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.c-price {
		color: #FF5656;
	}
	.bg-main {
		background: #01796d !important;
	}


	.br-15 {
		border-radius: 15rpx;
	}

	.br-20 {
		border-radius: 20rpx;
	}

	.br-41 {
		border-radius: 41rpx;
	}

	.br-48 {
		border-radius: 48rpx;
	}

	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}



	.idlabel {
		width: 80rpx;
		height: 38rpx;
		background: #666666;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
	}
	
</style>
<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-plus/index.scss";
</style>